# -*- coding: utf-8 -*-
# @Author: XiaoRui
# @FileName: dlltest.py
# @Time: 2021/4/30 15:05
# @Software: PyCharm

from ctypes import *
import datetime

MAX_SIZE = 100
LARGE_SIZE = 1000


class MapStruct(Structure):
    _fields_ = [("nodeName", c_int), ("road_osm_id", c_int*MAX_SIZE), ("road_osm_id_size", c_int), ("road_name", c_char*MAX_SIZE), ("road_type", c_double),
                ("road_wide", c_double*MAX_SIZE), ("road_wide_size", c_int), ("road_spide", c_double*MAX_SIZE), ("road_spide_size", c_int),
                ("road_length", c_longdouble), ("road_rtype", c_char*MAX_SIZE), ("road_shapeLeng", c_longdouble), ("node_coordinates", c_longdouble*2),
                ("node_childrenNodes", c_double*MAX_SIZE), ("node_childrenNodes_size", c_int),
                ("node_childrenNames", c_int*MAX_SIZE), ("node_childrenNames_size", c_int),
                ("nodelist_length", c_int)]


# 科目1情景1
class ExamMgsStructS1sj1(Structure):
    _fields_ = [("send_id", c_int), ("race_time", c_double), ("start_point", c_double*2), ("end_point", c_double*2)]


# 科目1情景2
class ExamMgsStructS1sj2(Structure):
    _fields_ = [("send_id", c_int), ("race_time", c_double), ("start_point", c_double*2), ("end_point", c_double*2), ("new_break_start_time", c_double)]


class BreaksMsgStruct(Structure):
    _fields_ = [("osm_id", c_int), ("break_type", c_int), ("break_start_point", c_double*2), ("break_end_point", c_double*2),
                ("happend_time", c_double), ("continue_time", c_double), ("break_level", c_int), ("breaks_msg_num", c_int), ("isNewBreaks", c_char*MAX_SIZE),
                ("subject", c_int)]


# 科目2情景1
class ExamMsgStructS2sj1(Structure):
    _fields_ = [("send_id", c_int), ("race_time", c_double), ("image_path", c_char*MAX_SIZE)]


# 科目2情景2--考试信息
class ExamMsgStructS2sj2(Structure):
    _fields_ = [("send_id", c_int), ("race_time", c_double), ("rescue_people_total", c_int)]


# 科目2情景2--救援民众信息
class RescuePeopleMsgStruct(Structure):
    _fields_ = [("people_id", c_int), ("people_type", c_int), ("people_point", c_double*2)]


# 科目三情景1--比赛信息和聚集地信息
class ExamMsgStructS3sj1(Structure):
    _fields_ = [("send_id", c_int), ("race_time", c_double),
                ("place_id", c_int), ("place_type", c_int), ("place_point", c_double*2), ("place_radius", c_double)]


def transfer(Objdll, node, node_length):
    simpStruct = MapStruct()

    # 节点名字
    simpStruct.nodeName = node.nodeName

    # osm_id
    for i in range(len(node.road_osm_id)):
        simpStruct.road_osm_id[i] = node.road_osm_id[i]

    # osm_id的长度
    simpStruct.road_osm_id_size = len(node.road_osm_id)

    # road name
    simpStruct.road_name = bytes(str(node.road_name), encoding="utf-8")

    # road type
    simpStruct.road_type = node.road_type

    # road wide
    for i in range(len(node.road_wide)):
        simpStruct.road_wide[i] = node.road_wide[i]
    simpStruct.road_wide_size = len(node.road_wide)

    # road spide
    for i in range(len(node.road_spide)):
        simpStruct.road_spide[i] = node.road_spide[i]
    simpStruct.road_spide_size = len(node.road_spide)

    # road length
    simpStruct.road_length = node.road_length

    # road rtype
    simpStruct.road_rtype = bytes(str(node.road_rtype), encoding="utf-8")

    # road shapeLeng
    simpStruct.road_shapeLeng = node.road_shapeLeng

    # node coordinates
    simpStruct.node_coordinates[0] = node.node_coordinates[0]
    simpStruct.node_coordinates[1] = node.node_coordinates[1]

    # node childrenNodes
    temp_index = 0
    for i in range(0, len(node.node_childrenNodes), 1):
        simpStruct.node_childrenNodes[temp_index] = node.node_childrenNodes[i][0]
        simpStruct.node_childrenNodes[temp_index+1] = node.node_childrenNodes[i][1]
        temp_index += 2
    simpStruct.node_childrenNodes_size = len(node.node_childrenNodes) * 2

    for i in range(len(node.node_childrenNames)):
        simpStruct.node_childrenNames[i] = node.node_childrenNames[i]
    simpStruct.node_childrenNames_size = len(node.node_childrenNames)

    simpStruct.nodelist_length = node_length

    # Objdll.recv_msg.argtypes = (POINTER(MapStruct), )
    # Objdll.recv_msg.restype = c_int
    # res = Objdll.recv_msg(byref(simpStruct))
    Objdll.recv_data.argtypes = (POINTER(MapStruct),)
    Objdll.recv_data.restype = c_int
    res = Objdll.recv_data(byref(simpStruct))
    # print(res)


def transfer_map(Objdll, nodes):
    for i in range(len(nodes)):
        for j in range(len(nodes[i])):
            transfer(Objdll, nodes[i][j], nodes[6][-1].nodeName + 1)
    return True


def transfer_exam_msg_s1(Objdll, client_path, subject):
    if subject == 1:
        # 情景1的断路信息
        res_breakmsg = transfer_breaks_msg(Objdll, client_path, "breaks", subject)
        # 情景1的考试信息
        examMgsStruct = ExamMgsStructS1sj1()
        examMgsStruct.send_id = client_path["send_id"]
        examMgsStruct.race_time = client_path["race_time"]
        examMgsStruct.start_point[0] = client_path["start_point"]["longitude"]
        examMgsStruct.start_point[1] = client_path["start_point"]["latitude"]
        examMgsStruct.end_point[0] = client_path["end_point"]["longitude"]
        examMgsStruct.end_point[1] = client_path["end_point"]["latitude"]
        Objdll.recv_exam_msg_S1sj1.argtypes = (POINTER(ExamMgsStructS1sj1),)
        Objdll.recv_exam_msg_S1sj1.restype = c_int
        res_exammsg = Objdll.recv_exam_msg_S1sj1(byref(examMgsStruct))
        if res_exammsg and res_breakmsg:
            return 1
    elif subject == 2:
        # 断路信息
        res_breakmsg = transfer_breaks_msg(Objdll, client_path, "breaks", subject)
        # 新的断路信息
        # res_nbreakmsg = transfer_breaks_msg(Objdll, client_path, "newBreaks", subject)
        # 考试信息
        examMgsStruct2 = ExamMgsStructS1sj2()
        examMgsStruct2.send_id = client_path["send_id"]
        examMgsStruct2.race_time = client_path["race_time"]
        examMgsStruct2.start_point[0] = client_path["start_point"]["longitude"]
        examMgsStruct2.start_point[1] = client_path["start_point"]["latitude"]
        examMgsStruct2.end_point[0] = client_path["end_point"]["longitude"]
        examMgsStruct2.end_point[1] = client_path["end_point"]["latitude"]
        # examMgsStruct2.new_break_start_time = client_path["new_break_start_time"]
        examMgsStruct2.new_break_start_time = 0
        Objdll.recv_exam_msg_S1sj2.argtypes = (POINTER(ExamMgsStructS1sj2),)
        Objdll.recv_exam_msg_S1sj2.restype = c_int
        res_exammsg = Objdll.recv_exam_msg_S1sj2(byref(examMgsStruct2))
        if res_breakmsg and 1 and res_exammsg:  # res_nbreakmsg
            return 1


def transfer_exam_msg_s2(Objdll, client_path, subject):
    if subject == 1:
        examMsgStruct_S2sj1 = ExamMsgStructS2sj1()
        examMsgStruct_S2sj1.send_id = client_path["send_id"]
        examMsgStruct_S2sj1.race_time = client_path["race_time"]
        examMsgStruct_S2sj1.image_path = bytes(str(client_path["image_path"]), encoding="utf-8")
        Objdll.recv_exam_msg_S2sb1.argtypes = (POINTER(ExamMsgStructS2sj1),)
        Objdll.recv_exam_msg_S2sb1.restype = c_int
        res = Objdll.recv_exam_msg_S2sb1(byref(examMsgStruct_S2sj1))
        if res:
            return 1
    elif subject == 2:
        # 考试信息
        examMsgStruct_S2sj2 = ExamMsgStructS2sj2()
        examMsgStruct_S2sj2.send_id = client_path["send_id"]
        examMsgStruct_S2sj2.race_time = client_path["race_time"]
        examMsgStruct_S2sj2.rescue_people_total = client_path["rescue_people_total"]
        Objdll.recv_exam_msg_S2sb2.argtypes = (POINTER(ExamMsgStructS2sj2),)
        Objdll.recv_exam_msg_S2sb2.restype = c_int
        res = Objdll.recv_exam_msg_S2sb2(byref(examMsgStruct_S2sj2))
        # 救援民众信息
        _count = 0
        res_people = 0
        rescuePeopleMsgStruct = RescuePeopleMsgStruct()
        for i in range(len(client_path["rescue_people"])):
            rescuePeopleMsgStruct.people_id = client_path["rescue_people"][i]["people_id"]
            rescuePeopleMsgStruct.people_type = client_path["rescue_people"][i]["people_type"]
            rescuePeopleMsgStruct.people_point[0] = client_path["rescue_people"][i]["people_point"]["longitude"]
            rescuePeopleMsgStruct.people_point[1] = client_path["rescue_people"][i]["people_point"]["latitude"]
            Objdll.recv_rescuePeople_msg.argtypes = (POINTER(RescuePeopleMsgStruct),)
            Objdll.recv_rescuePeople_msg.restype = c_int
            _count += Objdll.recv_rescuePeople_msg(byref(rescuePeopleMsgStruct))
            if _count == len(client_path["rescue_people"]):
                res_people = 1
        if res and res_people:
            return 1


def transfer_exam_msg_s3(Objdll, client_path, subject):
    if subject == 1:
        examMsgStruct_S3s1 = ExamMsgStructS3sj1()
        examMsgStruct_S3s1.send_id = client_path["send_id"]
        examMsgStruct_S3s1.race_time = client_path["race_time"]
        res = 0
        for i in range(len(client_path["place_points"])):
            examMsgStruct_S3s1.place_id = client_path["place_points"][i]["place_id"]
            examMsgStruct_S3s1.place_type = client_path["place_points"][i]["place_type"]
            examMsgStruct_S3s1.place_point[0] = client_path["place_points"][i]["place_point"]["longitude"]
            examMsgStruct_S3s1.place_point[1] = client_path["place_points"][i]["place_point"]["latitude"]
            examMsgStruct_S3s1.place_radius = client_path["place_points"][i]["place_radius"]
            Objdll.recv_exam_msg_S3sb1.argtypes = (POINTER(ExamMsgStructS3sj1),)
            Objdll.recv_exam_msg_S3sb1.restype = c_int
            res += Objdll.recv_exam_msg_S3sb1(byref(examMsgStruct_S3s1))
        # 传递断路信息
        # transfer_breaks_msg(Objdll, client_path, "breaks")
        # 民众信息
        _count = 0
        res_people = 0
        rescuePeople = RescuePeopleMsgStruct()
        for i in range(len(client_path["people"])):
            rescuePeople.people_id = client_path["people"][i]["people_id"]
            rescuePeople.people_type = client_path["people"][i]["people_type"]
            rescuePeople.people_point[0] = client_path["people"][i]["people_point"]["longitude"]
            rescuePeople.people_point[1] = client_path["people"][i]["people_point"]["latitude"]
            Objdll.recv_peopleMsg_s3s1.argtypes = (POINTER(RescuePeopleMsgStruct),)
            Objdll.recv_peopleMsg_s3s1.restype = c_int
            _count += Objdll.recv_peopleMsg_s3s1(byref(rescuePeople))
            if _count == len(client_path["people"]):
                res_people = 1
        return 1
    # 情景2
    else:
        pass


def transfer_breaks_msg(Objdll, client_path, breaks, subject):
    # tmpstruct = BreaksMsgStruct()
    # breaksMsgStruct = copy.deepcopy(tmpstruct)
    breaksMsgStruct = BreaksMsgStruct()
    res = 0
    for i in range(len(client_path[breaks])):
        breaksMsgStruct.osm_id = client_path[breaks][i]["osm_id"]
        breaksMsgStruct.break_type = client_path[breaks][i]["break_type"]
        breaksMsgStruct.break_start_point[0] = client_path[breaks][i]["break_start_point"]["longitude"]
        breaksMsgStruct.break_start_point[1] = client_path[breaks][i]["break_start_point"]["latitude"]
        breaksMsgStruct.break_end_point[0] = client_path[breaks][i]["break_end_point"]["longitude"]
        breaksMsgStruct.break_end_point[1] = client_path[breaks][i]["break_end_point"]["latitude"]
        breaksMsgStruct.happend_time = client_path[breaks][i]["happend_time"]
        breaksMsgStruct.continue_time = client_path[breaks][i]["continue_time"]
        breaksMsgStruct.break_level = client_path[breaks][i]["break_level"]
        breaksMsgStruct.breaks_msg_num = len(client_path[breaks])
        breaksMsgStruct.isNewBreaks = bytes(str(breaks), encoding="utf-8")
        breaksMsgStruct.subject = subject
        Objdll.recv_breaks_msg.argtypes = (POINTER(BreaksMsgStruct),)
        Objdll.recv_breaks_msg.restype = c_int
        res += Objdll.recv_breaks_msg(byref(breaksMsgStruct))
    if res == len(client_path[breaks]):
        return 1


def transfer_client_path(Objdll, client_path, session, subject):
    # 科目一
    if session == 1:
        res = transfer_exam_msg_s1(Objdll, client_path, subject)
        if res:
            return True
    # 科目二
    elif session == 2:
        res = transfer_exam_msg_s2(Objdll, client_path, subject)
        if res:
            return True
    # 科目三
    elif session == 3:
        res = transfer_exam_msg_s3(Objdll, client_path, subject)
        if res:
            return True
